﻿using System;
using System.Collections.Generic;
using Aspose.CAD;
using Aspose.CAD.FileFormats.Cad;
using Aspose.CAD.FileFormats.Cad.CadObjects;
using Aspose.CAD.FileFormats.Cad.CadConsts;
using Aspose.CAD.FileFormats.Cad.CadParameters;
using System.Text; // 包含 CadHeaderAttribute 枚举

namespace AsposeCadLimitsReader_V25_8
{
    class Program
    {
        static void Main(string[] args)
        {
            string dwgFilePath = @"pat\to\your\file.dwg"; // 替换为你的文件路径

            try
            {
                using (var image = Image.Load(dwgFilePath))
                {
                    if (image is CadImage cadImage)
                    {
                        var header = cadImage.Header;
                        if (header != null && header.HeaderProperties != null)
                        {
                            Cad3DPoint limMin = null;
                            Cad3DPoint limMax = null;
                            bool foundLimits = false;

                            // ✅ 方法 1: 使用 CadHeaderAttribute 枚举 (推荐，类型安全)
                            // 查找 $LIMMIN
                            if (header.HeaderProperties.TryGetValue(CadHeaderAttribute.LIMMIN, out var limMinParams) &&
                                limMinParams != null &&
                                limMinParams.Count > 0)
                            {
                                var lmp0 = limMinParams[0] as CadDoubleParameter;
                                var lmp1 = limMinParams[1] as CadDoubleParameter;

                                Console.WriteLine($"LIMMIN: {lmp0.Value} {lmp1.Value}");
                            }
                            else
                            {
                                Console.WriteLine("信息: 未找到或无法读取 $LIMMIN (CadHeaderAttribute.LimMin)。");
                            }

                            // 查找 $LIMMAX
                            if (header.HeaderProperties.TryGetValue(CadHeaderAttribute.LIMMAX, out var limMaxParams) &&
                                limMaxParams != null &&
                                limMaxParams.Count > 0)
                            {
                                var lmp0 = limMaxParams[0] as CadDoubleParameter;
                                var lmp1 = limMaxParams[1] as CadDoubleParameter;

                                Console.WriteLine($"LIMMAX: {lmp0.Value} {lmp1.Value}");
                            }
                            else
                            {
                                Console.WriteLine("信息: 未找到或无法读取 $LIMMAX (CadHeaderAttribute.LimMax)。");
                            }

                            // ✅ 方法 1: 使用 CadHeaderAttribute 枚举 (推荐，类型安全)
                            // 查找 $LIMMIN
                            if (header.HeaderProperties.TryGetValue(CadHeaderAttribute.PLIMMIN, out var plimMinParams) &&
                                plimMinParams != null &&
                                plimMinParams.Count > 0)
                            {
                                var lmp0 = plimMinParams[0] as CadDoubleParameter;
                                var lmp1 = plimMinParams[1] as CadDoubleParameter;

                                Console.WriteLine($"PLIMMIN: {lmp0.Value} {lmp1.Value}");
                            }
                            else
                            {
                                Console.WriteLine("信息: 未找到或无法读取 $LIMMIN (CadHeaderAttribute.LimMin)。");
                            }

                            // 查找 $LIMMAX
                            if (header.HeaderProperties.TryGetValue(CadHeaderAttribute.PLIMMAX, out var plimMaxParams) &&
                                plimMaxParams != null &&
                                plimMaxParams.Count > 0)
                            {
                                var lmp0 = plimMaxParams[0] as CadDoubleParameter;
                                var lmp1 = plimMaxParams[1] as CadDoubleParameter;

                                Console.WriteLine($"PLIMMAX: {lmp0.Value} {lmp1.Value}");
                            }
                            else
                            {
                                Console.WriteLine("信息: 未找到或无法读取 $LIMMAX (CadHeaderAttribute.LimMax)。");
                            }


                            // ✅ 方法 1: 使用 CadHeaderAttribute 枚举 (推荐，类型安全)
                            // 查找 $LIMMIN
                            if (header.HeaderProperties.TryGetValue(CadHeaderAttribute.EXTMIN, out var extMinParams) &&
                                extMinParams != null &&
                                extMinParams.Count > 0)
                            {
                                var lmp0 = extMinParams[0] as CadDoubleParameter;
                                var lmp1 = extMinParams[1] as CadDoubleParameter;

                                Console.WriteLine($"EXTMIN: {lmp0.Value} {lmp1.Value}");
                            }
                            else
                            {
                                Console.WriteLine("信息: 未找到或无法读取 $LIMMIN (CadHeaderAttribute.LimMin)。");
                            }

                            // 查找 $LIMMAX
                            if (header.HeaderProperties.TryGetValue(CadHeaderAttribute.EXTMAX, out var extMaxParams) &&
                                extMaxParams != null &&
                                extMaxParams.Count > 0)
                            {
                                var lmp0 = extMaxParams[0] as CadDoubleParameter;
                                var lmp1 = extMaxParams[1] as CadDoubleParameter;

                                Console.WriteLine($"EXTMAX: {lmp0.Value} {lmp1.Value}");
                            }
                            else
                            {
                                Console.WriteLine("信息: 未找到或无法读取 $LIMMAX (CadHeaderAttribute.LimMax)。");
                            }

                            Console.WriteLine($" ");
                            Console.WriteLine($" ");
                            foreach (var item in header.HeaderProperties)
                            {
                                if(item.Value.Count > 0)
                                {
                                    var values = new StringBuilder();
                                    foreach (var si in item.Value)
                                    {
                                        if(si is CadDoubleParameter cdp)
                                        {
                                            values.Append($"{cdp.Value},");
                                        }
                                    }

                                    Console.WriteLine($"{item.Key} : {values.ToString()}");
                                }
                            }

                            // 额外信息
                            Console.WriteLine($"\nAutoCAD 版本: {header.AcadVersion}");
                        }
                        else
                        {
                            Console.WriteLine("❌ 无法访问 DWG 文件头信息 (Header 或 HeaderProperties 为 null)。");
                        }
                    }
                    else
                    {
                        Console.WriteLine("❌ 加载的文件不是有效的 CAD 图像。");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ 发生错误: {ex.Message}");
            }

            Console.WriteLine("\n按任意键退出...");
            Console.ReadKey();
        }
    }
}